package iha.auh.kds.aba.painratingapp;

import iha.auh.kds.aba.painratingapp.Models.ChildModel;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ProgressDialog pd;
	
	private BroadcastReceiver dataReadyFromService =  new BroadcastReceiver() {
		@Override
        public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equalsIgnoreCase(DataService.VALIDATE_CPR)) {
				ChildModel child = ChildSingleton.getChild();
				
				String response = intent.getExtras().getString("response");
				
				pd.hide();
				
				int age = Utilities.extractAge(child.CPR);
				
				Intent painRatingIntent = new Intent(MainActivity.this, PainRatingActivity.class);
				
				if (child.ID == 0 || response.equals("failure")) {
					Toast.makeText(MainActivity.this, "Login faild", Toast.LENGTH_LONG).show();
				} else if (age < 4) {
					painRatingIntent.putExtra("FragmentPosition", 1);
					startActivity(painRatingIntent);
				} else if (age >= 4) {
					painRatingIntent.putExtra("FragmentPosition", 0);
					startActivity(painRatingIntent);
				}
			}
        }
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ChildSingleton.clear();
		pd = new ProgressDialog(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		LocalBroadcastManager
			.getInstance(this)
			.registerReceiver(dataReadyFromService, new IntentFilter(DataService.VALIDATE_CPR));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		LocalBroadcastManager
			.getInstance(this)
			.unregisterReceiver(dataReadyFromService);
	}
	
	public void loginClick(View view) {
		pd.setMessage("Kontroller CPR");
		pd.show();
		
		EditText inputEditText = (EditText) findViewById(R.id.social_security);
		String inputString = inputEditText.getText().toString().trim();
		inputString = Utilities.encodeString(inputString);
		
		Intent serviceIntent = new Intent(this, DataService.class);
		String url = "http://painratingrestful.cloudapp.net/" + 
					 "api/painrating/validatecpr?cpr=" + inputString;
		serviceIntent.putExtra("validate_cpr_url", url);
		startService(serviceIntent);
	}
	
	@Override
	public void onBackPressed() {
		// Should not be possible to go back from here
		// Should not be possible to go back to GraduationLine
		Toast.makeText(this, "Disabled.. Sorry :(", Toast.LENGTH_SHORT).show();
	}
}
